package uy.edu.um.ltic.client.communication;

import java.rmi.NotBoundException; 
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.List;

import uy.edu.um.ltic.commons.EmployeeRemoteMgt;
import uy.edu.um.ltic.commons.EmployeeVO;
import uy.edu.um.ltic.commons.ExceptionComm;
import uy.edu.um.ltic.commons.UtilUs;
import uy.edu.um.ltic.exception.DataBaseException;



public class EmployeeRemoteClientMgr implements EmployeeRemoteClientMgt{

	private static EmployeeRemoteClientMgr instance = null;

	private EmployeeRemoteClientMgr() {
	}

	public static EmployeeRemoteClientMgr getInstance() {
		if (instance == null) {
			instance = new EmployeeRemoteClientMgr();
		}
		return instance;
	}
	
	@Override
	public void deleteEmployee(int onEmployee)  throws ExceptionComm, DataBaseException {
		String sObjectService = "EmployeeMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			EmployeeRemoteMgt oEmployeeRemoteMgt = (EmployeeRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oEmployeeRemoteMgt.deleteEmployee(onEmployee);

		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		}
	}

	@Override
	public void editEmployee(EmployeeVO oEmployeeVO) throws ExceptionComm, DataBaseException {
		String sObjectService = "EmployeeMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			EmployeeRemoteMgt oEmployeeRemoteMgt = (EmployeeRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oEmployeeRemoteMgt.editEmployee(oEmployeeVO);

		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		}
	}

	@Override
	public List<EmployeeVO> getEmployees(EmployeeVO oEmployeeVO) throws ExceptionComm, DataBaseException {
		List<EmployeeVO> oListVO;
		String sObjectService = "EmployeeMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			EmployeeRemoteMgt oEmployeeRemoteMgt = (EmployeeRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oListVO = oEmployeeRemoteMgt.getEmployees(oEmployeeVO);
			
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oListVO;
	}

	@Override
	public void addEmployee(EmployeeVO dtEmployee) throws ExceptionComm, DataBaseException {
		String sObjectService = "EmployeeMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			EmployeeRemoteMgt oEmployeeRemoteMgt = (EmployeeRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oEmployeeRemoteMgt.addEmployee(dtEmployee);
					
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		
	}

	@Override
	public EmployeeVO getEmployeeLogIn(String username, char[] pass) throws ExceptionComm, DataBaseException {
		String sObjectService = "EmployeeMgt"; 
		Registry oRegitry;
		EmployeeVO oEmployeeVOF = null;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			EmployeeRemoteMgt oEmployeeRemoteMgt = (EmployeeRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			EmployeeVO oEmployeeVOR  = oEmployeeRemoteMgt.getEmployeeLogIn(username);
			if(oEmployeeVOR == null){// esto lo toque yo, por alguna razon pasa el if el EmploeeVOR cuando es null
				oEmployeeVOF = null;
			}else{
				String passSt = "";
				for(int i = 0; i < pass.length; i++){
					passSt = passSt + Character.toString(pass[i]);
				}
				String passCodified = UtilUs.getSecurePassword(passSt,oEmployeeVOR.getSalt()); 
				boolean ok = true;
				if (passCodified.length() == oEmployeeVOR.getPassword().length()){
					
					for(int i = 0; i < passCodified.length(); i++){
						if(oEmployeeVOR.getPassword().charAt(i) != passCodified.charAt(i)){
							ok = false;
						}
					}
				}else{
					ok = false;
				}
				if(ok){
					oEmployeeVOF = oEmployeeVOR;
				}else{
					oEmployeeVOF = new EmployeeVO(oEmployeeVOR.getUsername());
				}
			}
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oEmployeeVOF;
	}


	@Override
	public void setStatus(String status,int nEmployee) throws ExceptionComm, DataBaseException {
		String sObjectService = "EmployeeMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			EmployeeRemoteMgt oEmployeeRemoteMgt = (EmployeeRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oEmployeeRemoteMgt.setStatus(status, nEmployee);
			
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
			
	}

	@Override
	public EmployeeVO getEmployeeNEmp(int nEmployee) throws ExceptionComm, DataBaseException {
		EmployeeVO oEmployeeVO;
		String sObjectService = "EmployeeMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			EmployeeRemoteMgt oEmployeeRemoteMgt = (EmployeeRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oEmployeeVO = oEmployeeRemoteMgt.getEmployeeNEmp(nEmployee);
			
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oEmployeeVO;
	}

	@Override
	public ArrayList<EmployeeVO> getAllEmployees() throws ExceptionComm, DataBaseException {
		ArrayList<EmployeeVO> oListVO = new ArrayList<EmployeeVO>();
		String sObjectService = "EmployeeMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			EmployeeRemoteMgt oEmployeeRemoteMgt = (EmployeeRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oListVO = oEmployeeRemoteMgt.getAllEmployees();
			
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oListVO;
	}

}
